package com.hospital.warehouse.recovery.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hospital.util.AddDate;
import com.hospital.util.DTO;
import com.hospital.warehouse.exception.WarehouseException;
import com.hospital.warehouse.recovery.entity.Recovery;
import com.hospital.warehouse.recovery.service.RecoveryService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-28
 */
@Controller
@RequestMapping("/warehouse/recovery")
public class RecoveryController {

	@Autowired
	RecoveryService recoveryService;
	@Autowired
	AddDate addDate;

	@RequestMapping("/page")
	public String page(Model model) {
		// 添加回收单集合
		List<Recovery> recoveries = recoveryService.list();
		model.addAttribute("recoveries", recoveries);

		// 添加数据集合，用于搜索选择
		addDate.addDatas1(model);
		addDate.addDatas2(model);
		// 前往主页面
		return "views/warehouse/recovery.html";
	}

	@RequestMapping("/editpage")
	public String editpage(Model model, Integer id) {
		if (id != null) {
			Recovery po = recoveryService.queryById(id);
			model.addAttribute("po", po);
		}

		// 添加数据集合，用于搜索选择
		addDate.addDatas1(model);
		addDate.addDatas2(model);
		return "views/warehouse/recovery_edit.html";
	}

	@ResponseBody
	@RequestMapping("/paging")
	public DTO paging(Recovery po, Integer page, Integer limit) {
		DTO dto = new DTO();
		try {
			dto = recoveryService.paging(po, page, limit);
		} catch (ParseException e) {
			e.printStackTrace();
			dto.setMsg("日期错误");
		}
		return dto;
	}

	@ResponseBody
	@RequestMapping("/insertone")
	public Object insertOne(@RequestBody Recovery po, HttpSession session) {
		try {
			recoveryService.saveOne(po, session);
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "新增成功";
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam("ids[]") Integer[] ids) {
		try {
//			System.out.println(Arrays.asList(ids));
			recoveryService.removeByIds(Arrays.asList(ids));
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "删除成功";
	}

	@ResponseBody
	@RequestMapping("/updateone")
	public Object updateOne(@RequestBody Recovery po) {
		try {
			recoveryService.updateOne(po);
		} catch (WarehouseException e) {
			// 返回手动抛出的异常信息
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "修改成功";
	}

	@ResponseBody
	@RequestMapping("/updatestate")
	public Object updateState(Integer id, String state, HttpSession session) {
		try {
			recoveryService.updateState(id, state, session);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "修改成功";
	}
}
